var url = serverPre + "/manager/yzt/v1/preCustomer";
var loaded1 = false;

var bsConf = {

    queryFormId: "precustomer-query-form",

    createFormId: "precustomer-create-form",

    updateFormId: "precustomer-update-form",

    tableID: "id",

    /*    queryCols: 3,*/

    queryFloat: "right",

    voName: "preCustomerInfo",

    initData: true,

    queryConfigs:                   //页面头部查询form
        [
            // {
            //     type: 'select',
            //     label: '凭证类型',
            //     name: 'contactType',
            //     default: '',
            //     verify: '',
            //     cols: [[1, '手机'], [2, '邮箱'], [3, '固话'], [4, '用户标识']]
            // },

            {
                type: 'text',
                label: '用户号',
                name: 'preCustomerNo',
                default: '',
                verify: ''
            },
            {
                type: 'datetime',
                label: '创建时间',
                name: 'createTimeStart',
                // default: defaultCreateTime(),
                default: '',
                verify: ''
            },
            {
                type: 'datetime',
                label: '至',
                name: 'createTimeEnd',
                default: '',
                verify: ''
            },
            {
                type: 'text',
                label: '手机号',
                name: 'contactInfo',
                default: '',
                verify: ''
            },
            {
                type: 'select',
                label: '来源系统',
                name: 'subSystemSign',
                dataUrl: serverPre + "/manager/yzt/v1/system/dictInfo?accessed=1",
                // multiple: true,
                nameProperty: 'systemName',
                idProperty: 'systemSign'
            }
            // {
            //     type: 'datetime',
            //     label: '至',
            //     name: 'updateTimeEnd',
            //     default: '',
            //     verify: ''
            // }
        ],

    //列表内容
    tableCols: [
        {type: 'checkbox', width: '5%'}
        , {field: 'id', title: '序号', width: '5%', type: 'numbers'}
        , {field: 'preCustomerNo', width: '15%', title: '用户号'}
        , {field: 'contactType', width: '15%', title: '注册类型'}
        , {field: 'contactInfo', width: '15%', title: '手机号'}
        , {field: 'createTime', width: '15%', title: '用户号创建时间'}
        , {field: 'updateTime', width: '15%', title: '更新时间'}
        // , {fixed: 'right', width: 150, align: 'center', toolbar: '#barDemo'}
        , {
            field: 'action', width: '15%', title: '操作', templet: function (d) {
                return "" +
                    "  <button dataid='" + d.id + "' onclick=\"showPreUserDetail('" + d.preCustomerNo + "');\" class=\"layui-btn layui-btn-sm bs-button-update\">显示详情</button>\n" +
                    "";
            }
        }
    ],

    //templet: function (d) {return d.sts == '1' ? '启用' : '禁止'}
    //以下是各种回调
    //新建之后的回调
    // createCallback: function (result) {
    //     alert("createCallback:" + result.code);
    // },
    // //修改之后的回调  只针对默认实现
    // updateCallback: function (result) {
    //     alert("updateCallback:" + result.code);
    // },
    // //删除之后的回调  只针对默认实现
    // deleteCallback: function (result) {
    //     alert("deleteCallback:" + result.code);
    // },
    // //批量删除之后的回调
    // batchDeleteCallback: function (result) {
    //     alert("batchDeleteCallback:" + result.code);
    // },
    //查询之后的回调   这里是默认操作之后的补充操作
    // queryCallback: function (result) {
    //     if (!loaded1) {
    //         var theight = parseInt($(window).height());
    //         theight = (theight - 200) / 2;
    //
    //         $('#pie1').css('height', theight + 'px');
    //         $('#pie2').css('height', theight + 'px');
    //         initPie2();
    //         queryPieData();
    //     }
    //     loaded1 = true;
    //
    // },

}

var pied = false;

//显示用户详情
function showPreUserDetail(id) {
    $.ajax({
        type: 'get',
        url: serverPre + "/manager/yzt/v1/preCustomer/extend?preCustomerNo=" + id,
        dataType: 'json',
        headers: {"token": getCookie("token")},
        async: false,
        cache: false,
        success: function (result) {
            var tablec = "<table class=\"layui-table\">\n" +
                "  <colgroup>\n" +
                "    <col width=\"40\">\n" +
                "    <col width=\"60\">\n" +
                "    <col width=\"60\">\n" +
                "    <col width=\"60\">\n" +
                "    <col>\n" +
                "  </colgroup>\n" +
                "  <thead>\n" +
                "    <tr>\n" +
                "      <th>子系统标识</th>\n" +
                "      <th>创建时间</th>\n" +
                "      <th>客户号</th>\n" +
                "      <th>转化时间</th>\n" +
                "    </tr> \n" +
                "  </thead>\n" +
                "  <tbody>\n";
            if (result.data.preChanges.length > 0) {
                for (var i = 0; i < result.data.preChanges.length; i++) {
                    var v = result.data.preChanges[i];
                    tablec += "<tr><td>" + (v['subSystemSign'] == null ? "" : v['subSystemSign']) + "</td><td>" + (v['createTime'] == null ? "" : v['createTime']) + "</td><td>" + (v['customerNo'] == null ? "" : v['customerNo']) + "</td><td>" + (v['updateTime'] == null ? "" : v['updateTime']) + "</td></tr>\n";
                }
            } else {
                tablec += "<tr><td colspan='4' style='text-align: center'>无数据</td></tr> "
            }
            tablec += "</tbody></table>";

            var tablec2 = "<table class=\"layui-table\">\n" +
                "  <colgroup>\n" +
                "    <col width=\"40\">\n" +
                "    <col width=\"160\">\n" +
                "    <col>\n" +
                "  </colgroup>\n" +
                "  <thead>\n" +
                "    <tr>\n" +
                "      <th>子系统标识</th>\n" +
                "      <th>扩展信息</th>\n" +
                "    </tr> \n" +
                "  </thead>\n" +
                "  <tbody>\n";
            if (result.data.preExtends.length > 0) {
                for (var i = 0; i < result.data.preExtends.length; i++) {
                    var v = result.data.preExtends[i];
                    tablec2 += "<tr><td>" + (v['subSystemSign'] == null ? "" : v['subSystemSign']) + "</td><td>" + (v['remark'] == null ? "" : v['remark']) + "</td></tr>\n";
                }
            } else {
                tablec2 += "<tr><td colspan='2' style='text-align: center'>无数据</td></tr> "
            }
            tablec2 += "</tbody></table>";

            var content = "<div style='margin: 10px;'>用户号：" + id +
                "<blockquote class=\"layui-elem-quote\">用户转化详情：</blockquote>" + tablec + "<br/><br/>" +
                "<blockquote class=\"layui-elem-quote\">用户扩展信息：</blockquote>" + tablec2 + "<br/><br/>" +
                "</div>";
            layer.open({
                type: 1,
                title: '用户详情信息',
                shadeClose: true,
                shade: false,
                maxmin: true,
                content: content,
                area: ['80%', '70%'],
                success: function () {

                }
            });
        }
    });

}

//查询饼图数据
function queryPieData() {
    if (pied == false) {
        $.ajax({
            type: 'get',
            url: serverPre + "/manager/yzt/v1/preCustomer/static",
            dataType: 'json',
            headers: {"token": getCookie("token")},
            async: true,
            cache: false,
            success: function (result) {
                var pie1 = echarts.init(document.getElementById('pie1'));
                var map = result.data;

                // 指定图表的配置项和数据
                var option = {
                    color: ["#2dc7c9", "#e87c25"],
                    // title : {
                    //     text: '某站点用户访问来源',
                    //     subtext: '纯属虚构',
                    //     x:'center'
                    // },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: ['已转化用户', '未转化用户']
                    },
                    series: [
                        {
                            name: '转化率',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '60%'],
                            data: [
                                {value: map['changed'], name: '已转化用户'},
                                {value: map['total'] - map['changed'], name: '未转化用户'},
                            ],
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
                // 使用刚指定的配置项和数据显示图表。
                pie1.setOption(option);
            }
        });
    }
}

function initPie2() {
    var theight = parseInt($(window).height());
    theight = (theight - 550) / 2;
    var twidth = parseInt($(window).width());
    twidth = twidth / 4;
    if (theight > twidth / 4) {
        theight = twidth / 4;
    }
    if (theight < 40) {
        theight = 40;
    }
    var changed = 0;
    var total = 0;
    var dataStyle = {
        normal: {
            label: {show: false},
            labelLine: {show: false}
        }
    };
    var placeHolderStyle = {
        normal: {
            color: 'rgba(0,0,0,0)',
            label: {show: false},
            labelLine: {show: false}
        },
        emphasis: {
            color: 'rgba(0,0,0,0)'
        }
    };

    option = {
        color: ["#e87c25", "#2dc7c9"],
        title: {
            text: '单个客户转换情况',

            x: 'center',
            y: 'center',
            itemGap: 20,
            textStyle: {
                color: 'rgba(30,144,255,0.8)',
                fontFamily: '微软雅黑',
                fontSize: theight / 5.5,
                fontWeight: 'bolder'
            }
        },
        /*   tooltip : {
               show: true,
               formatter: "{a} <br/>{b} : {c} ({d}%)"
           },*/
        legend: {
            orient: 'vertical',
            x: 0,
            y: 0,
            itemGap: 12,
            data: [total + '个系统注册来源', changed + '个系统已转换']
        },
        /* toolbox: {
             show : true,
             feature : {
                 mark : {show: true},
                 dataView : {show: true, readOnly: false},
                 restore : {show: true},
                 saveAsImage : {show: true}
             }
         },*/
        series: [
            {
                name: '系统注册来源',
                type: 'pie',
                clockWise: false,
                radius: [theight, theight + 25],
                itemStyle: dataStyle,
                data: [
                    {
                        value: total == 0 ? 75 : total,
                        name: total + '个系统注册来源'
                    },
                    {
                        value: total == 0 ? 25 : total / 3,
                        name: '',
                        itemStyle: placeHolderStyle
                    }
                ]
            },
            {
                name: '系统已转换',
                type: 'pie',
                clockWise: false,
                radius: [theight - 25, theight],
                itemStyle: dataStyle,
                data: [
                    {
                        value: changed == 0 ? (total == 0 ? 75 : 1) : changed,
                        name: changed + '个系统已转换'
                    },
                    {
                        value: changed == 0 ? (total == 0 ? 25 : 100) : (total / 3 * 4 - changed),
                        name: '',
                        itemStyle: placeHolderStyle
                    }
                ]
            }
        ]
    };

    require.config({
        paths: {
            echarts: '../echarts/build/dist'
        }
    });
    require(
        [
            'echarts',
            'echarts/chart/pie',   // 按需加载所需图表，如需动态类型切换功能，别忘了同时加载相应图表

        ],
        function (ec) {
            var mychart = echarts2.init(document.getElementById("pie2"));
            mychart.setOption(option);

        }
    );
}

function queryPieData2(prekey) {
    var theight = parseInt($(window).height());
    theight = (theight - 550) / 2;
    var twidth = parseInt($(window).width());
    twidth = twidth / 4;
    if (theight > twidth / 4) {
        theight = twidth / 4;
    }
    if (theight < 40) {
        theight = 40;
    }
    if (pied == false) {
        $.ajax({
            type: 'get',
            url: serverPre + "/manager/yzt/v1/preCustomer/change?preCustomerNo=" + prekey,
            dataType: 'json',
            headers: {"token": getCookie("token")},
            async: true,
            cache: false,
            success: function (result) {

                var map = result.data;

                var changed = map['changed'] == null ? 0 : map['changed']
                var total = map['total'] == null ? 0 : map['total']

                // 指定图表的配置项和数据
                var dataStyle = {
                    normal: {
                        label: {show: false},
                        labelLine: {show: false}
                    }
                };
                var placeHolderStyle = {
                    normal: {
                        color: 'rgba(0,0,0,0)',
                        label: {show: false},
                        labelLine: {show: false}
                    },
                    emphasis: {
                        color: 'rgba(0,0,0,0)'
                    }
                };

                option = {
                    color: ["#e87c25", "#2dc7c9"],
                    title: {
                        text: '单个客户转换情况',

                        x: 'center',
                        y: 'center',
                        itemGap: 20,
                        textStyle: {
                            color: 'rgba(30,144,255,0.8)',
                            fontFamily: '微软雅黑',
                            fontSize: theight / 5.5,
                            fontWeight: 'bolder'
                        }
                    },
                    /*   tooltip : {
                           show: true,
                           formatter: "{a} <br/>{b} : {c} ({d}%)"
                       },*/
                    legend: {
                        orient: 'vertical',
                        x: 0,
                        y: 0,
                        itemGap: 12,
                        data: [total + '个系统注册来源', changed + '个系统已转换']
                    },
                    /* toolbox: {
                         show : true,
                         feature : {
                             mark : {show: true},
                             dataView : {show: true, readOnly: false},
                             restore : {show: true},
                             saveAsImage : {show: true}
                         }
                     },*/
                    series: [
                        {
                            name: '系统注册来源',
                            type: 'pie',
                            clockWise: false,
                            radius: [theight, theight + 25],
                            itemStyle: dataStyle,
                            data: [
                                {
                                    value: total == 0 ? 75 : total,
                                    name: total + '个系统注册来源'
                                },
                                {
                                    value: total == 0 ? 25 : total / 3,
                                    name: '',
                                    itemStyle: placeHolderStyle
                                }
                            ]
                        },
                        {
                            name: '系统已转换',
                            type: 'pie',
                            clockWise: false,
                            radius: [theight - 25, theight],
                            itemStyle: dataStyle,
                            data: [
                                {
                                    value: changed == 0 ? (total == 0 ? 75 : 1) : changed,
                                    name: changed + '个系统已转换'
                                },
                                {
                                    value: changed == 0 ? (total == 0 ? 25 : 100) : (total / 3 * 4 - changed),
                                    name: '',
                                    itemStyle: placeHolderStyle
                                }
                            ]
                        }
                    ]
                };

                require.config({
                    paths: {
                        echarts: '../echarts/build/dist'
                    }
                });
                require(
                    [
                        'echarts',
                        'echarts/chart/pie',   // 按需加载所需图表，如需动态类型切换功能，别忘了同时加载相应图表

                    ],
                    function (ec) {
                        var mychart = echarts2.init(document.getElementById("pie2"));
                        mychart.setOption(option);

                    }
                );
            }
        });
    }
}

function keyup_submit(e) {
    var evt = window.event || e;
    if (evt.keyCode == 13) {
        var prekey = $('#prekey').val();
        if (isNull(prekey)) {
            alert("请输入用户号");
        }
        else {
            queryPieData2(prekey);
        }
    }
}

function keyup1_submit() {
    var prekey = $('#prekey').val();
    if (isNull(prekey)) {
        alert("请输入用户号");
    }
    else {
        queryPieData2(prekey);
    }
}

function keyup_search() {
    $("#precustomer-query-form").toggle()
}

